package net.st.controller.data;

import net.st.meta.SearchDataMeta;
import net.st.service.SearchDataService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;
import org.slim3.util.RequestMap;

public class UpdateController extends Controller {

	private SearchDataService service = new SearchDataService();

	private SearchDataMeta meta = SearchDataMeta.get();

	@Override
	public Navigation run() throws Exception {
		if (!validate()) {
			return forward("edit.jsp");
		}
		service.update(asKey(meta.key), asLong(meta.version), new RequestMap(
				request));
		return redirect(basePath);
	}

	protected boolean validate() {
		Validators v = new Validators(request);
		v.add(meta.query, v.required());
		v.add(meta.url, v.required());
		v.add(meta.newHtmlDataXPath, v.required());
		v.add(meta.md5XPath, v.required());
		return v.validate();
	}
}